apiready = function() {
    seajs.use(['../../V', "../../api", "../../jquery", "../../app", "../../service", "../../swiper", "../../dialog"], function(V, $api, $, app, service, Swiper, dialog) {
      app.ready();
        var vm = {
            el: '.vue-content',
            data: {
                goodsObj: {}, //关联商品的信息
                titleContent: '', //商品亮点
                description: '',
                swiperImageArr: [], //轮播图的图片数组
                edit: '',
                goodsList: [],
                textNum: 0,
                goods_id: '',
                shopId: ''
            },
            created: function() {
                var that = this;
                this.shopId = api.pageParam.shopId; // 商店id
                var unionid = $api.getStorage("shop_unionid"); // 用户id
                if(api.pageParam.goodsObj){
                  this.goodsObj = api.pageParam.goodsObj; // 商品信息
                  this.titleContent = this.goodsObj.intro;
                }

                //--键盘监听（解决ios键盘弹出后顶部样式混乱的问题）
                api.addEventListener({
                    name: 'keyboardshow'
                }, function(ret, err) {
                    $('.header').css({
                        'top': 0,
                        'position': 'static'
                    });
                });
                api.addEventListener({
                    name: 'keyboardhide'
                }, function(ret, err) {
                    $('.header').css({
                        'top': 0,
                        'position': 'fixed'
                    });
                });


            },
            mounted: function() {
                var that = this;

                //--限制商品标题输入（50个字符）
                $(".shop_add_text").keyup(function() {
                    that.textNum = that.titleContent.length;
                    // console.log(that.textNum)
                    if (that.textNum >= 50) {
                        that.textNum = 50;
                        that.titleContent = that.titleContent.substring(0, 50)
                        return false;
                    }
                });
                //--保证在输入中文时也会实时更新textNum
                var eventName = (navigator.userAgent.indexOf("MSIE") != -1) ? "propertychange" : "input";
                $(".shop_add_text").bind(eventName, function() {
                    that.textNum = that.titleContent.length;
                });

                //--解决ios输入法挡住输入框的问题
                $(document).on("focus", "#aui-input", function() {
                    setTimeout(function() {
                        console.log(document.body.offsetHeight);
                        window.scrollTo(0, document.body.offsetHeight - 280);
                    }, 50);
                });

                // 监听是否选中了商品
                api.addEventListener({
                    name: 'getGoodsId'
                }, function(ret, err) {
                    if (ret.value) {
                        if (ret.value.goodsId) {
                            api.toast({
                                msg: '关联成功',
                                duration: 2000,
                                location: 'middle'
                            });
                        }

                        // 商品id
                        var goodsId = ret.value.goodsId;
                        var goodsName = ret.value.goodsName;
                        var cover = ret.value.cover;
                        var price = ret.value.price;

                        that.$set(that.goodsObj, 'name', goodsName);
                        that.$set(that.goodsObj, 'cover', cover);
                        that.$set(that.goodsObj, 'price', price);
                        that.$set(that.goodsObj, 'goods_id', goodsId);
                    } else {
                        api.toast({
                            msg: '关联失败',
                            duration: 2000,
                            location: 'middle'
                        });
                    }
                });
            },

            methods: {
                //--点击返回按钮事件
                close: function() {
                    api.closeWin({
                        name: 'applyToExposure'
                    });
                },
                //--添加商品链接
                addGoodsLink: function() {
                    var shopId = this.shopId;

                    app.openWin('shopCard_Link', {
                        index: 0,
                        shopId: shopId
                    });
                },
                //--保存或者编辑商品信息，上传到接口
                saveOrEditGoods: function() {
                    var shopId = this.shopId; // 商店id
                    var unionid = $api.getStorage("shop_unionid"); // 用户id

                    var goods_id = this.goodsObj.goods_id;
                    var goods_desc = this.titleContent;

                    var that = this;

                    if (goods_id == '' || goods_id == null) {
                        api.toast({
                            msg: '请选择商品',
                            duration: 2000,
                            location: 'middle'
                        });
                        return;
                    }

                    if (this.edit != 1) {
                        app.ajax(service['applyToRecommend'], {
                                data: {
                                    values: {
                                        shop_code: shopId,
                                        unionid: unionid,
                                        goods_id: goods_id,
                                        intro: goods_desc
                                    }
                                }
                            },
                            function(res) {
                                // var shopCode = res.data.shop_code; // 商店id
                                if (res.code) {
                                    api.toast({
                                        msg: res.message,
                                        duration: 2000,
                                        location: 'middle'
                                    });
                                } else {
                                    alert("申请成功!\n申请需要审核，请耐心等待...");
                                    app.ajax(service['getApplyRecommend'], {
                                        data: {
                                            values: {
                                                shop_code: shopId
                                            }
                                        }
                                    }, function(res) {
                                        api.sendEvent({
                                            name: 'exposureList',
                                            extra: {
                                                goodsObj: res.data
                                            }
                                        });
                                        api.closeWin()
                                    })
                                }
                            });
                    } else {
                        app.ajax(service['goodsEdit'], {
                                data: {
                                    values: {
                                        shop_code: shopId,
                                        unionid: unionid,
                                        images: images,
                                        description: description
                                    }
                                }
                            },
                            function(res) {
                                // var shopCode = res.data.shop_code; // 商店id
                                if (res.code) {
                                    api.toast({
                                        msg: res.message,
                                        duration: 2000,
                                        location: 'middle'
                                    });
                                } else {
                                    if (yzcTag) {
                                        alert("编辑商品成功!\n发布YZC商品需要审核，请耐心等待...");
                                    } else {
                                        alert("编辑商品成功!");
                                    }
                                    //--发送事件（使商品管理页面刷新）
                                    api.sendEvent({
                                        name: 'backToGoodsManage',
                                    });
                                }
                            });
                    }
                },
                //--打开上传图片页面
                openImgManage: function() {
                    app.openWin('shopAddImgManage', {
                        imgArr: this.swiperImageArr
                    })
                },
                //--批量上传图片
                getPictureS: function() {
                    var that = this;
                    app.getPictureS(8, this.num, service['fileupload'], function(res) {
                        if (res.length > 0) {
                            api.toast({
                                msg: '上传成功',
                                duration: 2000,
                                location: 'middle'
                            });
                            // console.log("我传的图片字符串到底是什么："+res+" typeof: "+ res.constructor +"1111\n");
                            // var str1 = res.join("|");
                            // if(that.imgarr==''){
                            // 	that.imgarr = str1;
                            // }else{
                            // 	that.imgarr = that.imgarr + '|' + str1;
                            // }
                            // console.log("往接口传的图片字符串到底是什么："+that.imgarr+" typeof: "+ typeof that.imgarr +"222\n");

                            //前端为轮播图增加class
                            that.bigger = true;
                            that.small = true;
                            that.num = that.num + res.length;

                            that.swiperImageArr.push(res);
                            console.log("第一步：" + that.swiperImageArr + " typeof: " + that.swiperImageArr.constructor + "1111\n");
                            var str4 = that.swiperImageArr.join(",");
                            console.log("第一步：" + str4 + " typeof: " + str4.constructor + "1111\n");
                            that.swiperImageArr = str4.split(",");
                            console.log("第三步：" + that.swiperImageArr + " typeof: " + that.swiperImageArr.constructor + "1111\n");
                            // console.log(that.swiperImageArr[0]+" &Length:"+that.swiperImageArr.length+" &typeof: "+typeof that.swiperImageArr +"\n")
                            // console.log(JSON.stringify(that.swiperImageArr) +" : "+that.swiperImageArr.constructor);
                            that.$nextTick(function() {
                                that.mySwiper.destroy(false);
                                $(".swiper-pagination").css("display", "block")
                            })
                        }
                    })
                },

            }
        };
        // var shopVue = new Vue(vm);
        var even = function() {


        };

        var newVue = new Vue(vm);
        even();

    })
}
